package com.carboy.launch.utils;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

import android.annotation.SuppressLint;
import android.text.TextUtils;
import android.text.format.Time;

/**
 * 时间常用类
 */
public class DateUtils {

	/*
	 * static final String[] dataFormat = new String[] { "yyyy-MM-dd HH:mm:ss",
	 * "yyyyMMddHHmmss", "yyyy年MM月dd日 HH时mm分ss秒", "yyyy-MM-dd", "yyyy", "MM",
	 * "dd", "HH", "mm", "ss" };
	 */

	/**
	 * 将时间戳转换成对应的格式
	 * 
	 * @param time
	 *            需要转换的时间
	 * @param format_stirng
	 *            转换的格式
	 * @return 返回转换后对应格式的时间
	 */
	public static String getStrTime(String time, String format_stirng) {
		String retime = null;
		try {
			if (time.length() != 0) {
				SimpleDateFormat sDataFormat = new SimpleDateFormat(
						format_stirng);
				long lTime = Long.valueOf(time);
				retime = sDataFormat.format(new Date(lTime));
			} else {
				// 返回原数据
				retime = time;
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return retime;
	}

	/**
	 * 获取当前日期的字符串
	 * 
	 * @param format_string
	 *            时间格式，譬如："yyyy-MM-dd HH:mm:ss"
	 * @return 字符串
	 */
	public static final String getDateNow(String format_string) {
		if ((format_string == null) || (format_string.equals("")))
			format_string = "yyyy-MM-dd HH:mm:ss";

		Calendar cld = Calendar.getInstance();
		DateFormat df = new SimpleDateFormat(format_string);
		return df.format(cld.getTime());
	}

	/**
	 * 传入时间返回 HH:mm:ss格式
	 * 
	 * @param time
	 *            传入时间
	 * @return
	 */
	public static String secToTime(int time) {
		String timeStr = null;
		int hour = 0;
		int minute = 0;
		int second = 0;
		if (time <= 0)
			return "00:00";
		else {
			minute = time / 60;
			if (minute < 60) {
				second = time % 60;
				timeStr = unitFormat(minute) + ":" + unitFormat(second);
			} else {
				hour = minute / 60;
				if (hour > 99)
					return "99:59:59";
				minute = minute % 60;
				second = time - hour * 3600 - minute * 60;
				timeStr = unitFormat(hour) + ":" + unitFormat(minute) + ":"
						+ unitFormat(second);
			}
		}
		return timeStr;
	}

	public static String unitFormat(int i) {
		String retStr = null;
		if (i >= 0 && i < 10)
			retStr = "0" + Integer.toString(i);
		else
			retStr = "" + i;
		return retStr;
	}

	/**
	 * 
	 * @author James
	 * @Description 获取str时间-年月日
	 * @param mDate
	 * @param format
	 * @return
	 */
	@SuppressLint("SimpleDateFormat")
	public static String getNetDate(Date mDate, String format) {

		if (mDate != null) {
			if (!TextUtils.isEmpty(format)) {
				String strTime = null;
				SimpleDateFormat df = new SimpleDateFormat(format);// 设置日期格式
				strTime = df.format(mDate);
				return strTime;
			}
		}
		return null;
	}

	/**
	 * 
	 * @author James
	 * @Description 获取网络的日期，只获取日
	 * @param mDate
	 * @return
	 */
	public static String getNetDay(Date mDate) {

		DateFormat df = DateFormat.getDateInstance();
		String s = df.format(mDate);
		return s;
	}

	/**
	 * 距离今天多久
	 * 
	 * @param date
	 * @return
	 * 
	 */
	public static String fromToday(Date date) {
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		int ampm = calendar.get(Calendar.AM_PM);
		String ap = ampmStr(ampm);
		int hour = calendar.get(Calendar.HOUR);
		int miute = calendar.get(Calendar.MINUTE);
		long time = date.getTime() / 1000;
		long now = new Date().getTime() / 1000;
		long ago = now - time;
		if (ago <= ONE_HOUR) {
			if (ago / ONE_MINUTE <= 0) {
				return "主人，您的爱车在刚刚出现过振动";
			} else {
				return "主人，您的爱车在" + ago / ONE_MINUTE + "分钟前出现过振动";
			}
		}
		// 今天
		else if (ago <= ONE_DAY) {
			return "主人，您的爱车在今天" + ap + hour + "点" + miute + "分" + "出现过振动";
			// 昨天
		} else if (ago <= ONE_DAY * 2) {
			return "主人，您的爱车在昨天" + ap + hour + "点" + miute + "分" + "出现过振动";
			// 前天
		} else if (ago <= ONE_DAY * 3) {
			return "主人，您的爱车在前天" + ap + hour + "点" + miute + "分" + "出现过振动";
			// 几天前
		} else if (ago <= ONE_MONTH) {
			long day = ago / ONE_DAY;
			return "主人，您的爱车在" + day + "天前出现过振动";
		}
		return null;
	}

	private static final long ONE_MINUTE = 60;
	private static final long ONE_HOUR = 3600;
	private static final long ONE_DAY = 86400;
	private static final long ONE_MONTH = 2592000;
	private static final long ONE_YEAR = 31104000;

	public static String ampmStr(int ampm) {
		if (ampm == Calendar.AM) {
			return "上午";
		} else if (ampm == Calendar.PM) {
			return "下午";
		}
		return null;
	}
}
